package org.example.erp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.erp.domain.ProcedureDetails;
import org.example.erp.mapper.ProcedureDetailsMapper;
import org.example.erp.service.ProceduresDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ProceduresDetailsServiceImpl extends ServiceImpl<ProcedureDetailsMapper, ProcedureDetails> implements ProceduresDetailsService {
    @Transactional
    public boolean add(ProcedureDetails procedureDetails) {
        return processMethod(procedureDetails,"添加");
    }

    @Transactional
    public boolean updata(ProcedureDetails procedureDetails) {
        return processMethod(procedureDetails,"修改");
    }

    private boolean processMethod(ProcedureDetails procedureDetails,String name) {
        try {
            boolean condition=false;
            if (name.equals("修改")){
                // 修改工序详情
                 condition = updateById(procedureDetails);
            }else if (name.equals("添加")){
                // 添加新的工序详情
                 condition = save(procedureDetails);
            }
            if (!condition) {
                log.error("未能"+name+"详细工序信息: {}", procedureDetails);
                return false;
            }

            // 修改上一个相邻序号的某个字段
            ProcedureDetails adjacentDetails = getAdjacentId(procedureDetails.getProcedureCode(), procedureDetails.getOrderNumber()-1);
            if (adjacentDetails != null) {
                // 假设要修改的字段是 `status`
                adjacentDetails.setNextProcessCode(procedureDetails.getProcessCode());
                adjacentDetails.setNextProcessName(procedureDetails.getProcessName());
                updateById(adjacentDetails);
            }
            // 如果有下一个相邻序号，修改本数据的某个字段
            ProcedureDetails nextAdjacentDetails = getAdjacentId(procedureDetails.getProcedureCode(), procedureDetails.getOrderNumber()+1);
            if (nextAdjacentDetails != null) {
                // 假设要修改的字段是 `status`
                procedureDetails.setNextProcessCode(nextAdjacentDetails.getProcessCode());
                procedureDetails.setNextProcessName(nextAdjacentDetails.getProcessName());
                updateById(procedureDetails);
            }else if(nextAdjacentDetails==null){
                procedureDetails.setNextProcessCode("");
                procedureDetails.setNextProcessName("");
                updateById(procedureDetails);
            }

            log.info("已成功"+name+"工序详细信息: {}", procedureDetails);
            return true;
        } catch (Exception e) {
            log.error(name+"工序详细信息时出错: {}", procedureDetails, e);
            return false;
        }
    }

    private ProcedureDetails getAdjacentId(String procedureCode, int orderNumber) {
        // 假设这里通过某种逻辑获取相邻序号的ID
        ProcedureDetails process = baseMapper.findProcess(procedureCode, orderNumber);
        // 这里只是一个示例，具体逻辑需要根据实际情况实现
        return process; // 示例：获取下一个序号的ID
    }

    public List<ProcedureDetails> taskFindProcess(String productCode){
        return baseMapper.taskFindProcess(productCode);
    }

    public ProcedureDetails findEndProcess(String productCode){
        return baseMapper.findEndProcess(productCode);
    }
}
